import sys

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QInputDialog, QHBoxLayout, QVBoxLayout, QTableWidget, QTableWidgetItem
from PyQt5.QtGui import QFont
from PyQt5.QtCore import pyqtSlot
from FileOperate import ExcelOperate
from model import Student

'''
老师具有的操作:删除学生信息、添加学生信息、更新学生
'''
class TeacherSysWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("教师系统")
        self.resize(850, 500)
        self.move(500, 200)

        # 查询所有学生的信息
        excel_operate_obj = ExcelOperate("stuLists.xlsx")
        all_students = excel_operate_obj.read_sheet("stuLists")

        # 创建一个用于显示学生信息的表格
        self.information_table = QTableWidget(len(all_students), 8)
        self.information_table.setHorizontalHeaderLabels(['姓名', '年龄', '性别', '籍贯', '学号', '学院', '专业', '密码'])
        # 向表格中加入信息
        row = 0

        # 所有的单选按钮加到一个group中实现互斥
        for stu in all_students:
            self.information_table.setItem(row, 0, QTableWidgetItem(stu.get_name()))
            self.information_table.setItem(row, 1, QTableWidgetItem(str(stu.get_age())))
            self.information_table.setItem(row, 2, QTableWidgetItem(stu.get_sex()))
            self.information_table.setItem(row, 3, QTableWidgetItem(stu.get_birthPlace()))
            self.information_table.setItem(row, 4, QTableWidgetItem(str(stu.get_id())))
            self.information_table.setItem(row, 5, QTableWidgetItem(stu.get_college()))
            self.information_table.setItem(row, 6, QTableWidgetItem(stu.get_major()))
            self.information_table.setItem(row, 7, QTableWidgetItem(stu.get_password()))
            row += 1
        
        # 垂直布局管理器
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.information_table)

        self.add_button = QPushButton("添加新生信息")
        self.del_button = QPushButton("删除学生信息")
        self.update_button = QPushButton("修改学生信息")

        # 连接槽函数
        self.add_button.clicked.connect(self.on_click_add_button)
        self.del_button.clicked.connect(self.on_click_del_button)
        self.update_button.clicked.connect(self.on_click_update_button)

        # 水平布局管理器
        self.hbox = QHBoxLayout()
        self.hbox.addWidget(self.add_button)
        self.hbox.addWidget(self.del_button)
        self.hbox.addWidget(self.update_button)

        # 垂直布局管理器
        self.vbox_of_window = QVBoxLayout()
        self.vbox_of_window.addLayout(self.vbox)
        self.vbox_of_window.addLayout(self.hbox)

        # 设置窗口的布局管理器
        self.setLayout(self.vbox_of_window)

        # 设置窗口显示
        self.show()
    
    """
    信号槽函数
    """
    @pyqtSlot()
    def on_click_add_button(self):
        self.sub_window = QWidget()
        self.sub_window.setWindowTitle("添加新学生")
        self.sub_window.move(800, 300)

        self.add_table = QTableWidget(7, 1)
        self.add_table.setVerticalHeaderLabels(['姓名', '年龄', '性别', '籍贯', '学号', '学院', '专业'])
        self.add_table_hbox = QHBoxLayout()
        self.add_table_hbox.addWidget(self.add_table)

        self.ok_btn = QPushButton("确认")
        self.ok_btn_hbox = QHBoxLayout()
        self.ok_btn_hbox.addWidget(self.ok_btn)

        self.sub_window_vbox = QVBoxLayout()
        self.sub_window_vbox.addLayout(self.add_table_hbox)
        self.sub_window_vbox.addLayout(self.ok_btn_hbox)

        self.sub_window.setLayout(self.sub_window_vbox)

        self.ok_btn.clicked.connect(self.on_click_ok_btn)
        self.sub_window.show()

    @pyqtSlot()
    def on_click_ok_btn(self):
        name = self.add_table.item(0, 0).text()
        age = self.add_table.item(1, 0).text()
        sex = self.add_table.item(2, 0).text()
        birthPlace = self.add_table.item(3, 0).text()
        stu_id = self.add_table.item(4, 0).text()
        college = self.add_table.item(5, 0).text()
        major = self.add_table.item(6, 0).text()
        new_stu = Student(name, age, sex, birthPlace, stu_id, college, major, "1")

        excel_operate_obj = ExcelOperate("./stuLists.xlsx");
        excel_operate_obj.add_new_stu("stuLists", new_stu)

        self.sub_window.hide()
        self.hide()
        self.new_teawindow = TeacherSysWindow()
        self.new_teawindow.show()

    @pyqtSlot()
    def on_click_del_button(self):
        id, ok = QInputDialog.getText(self, '确认', '输入学生学号:')
        if ok:
            excel_operate_obj = ExcelOperate("stuLists.xlsx")
            excel_operate_obj.delete_byID("stuLists", id)

        # 使用笨方法进行界面更新，不建议使用这个方法
        # 最好是使用多线程的方法进行界面更新，避免闪烁和卡顿
        self.hide()
        self.new_teawindow = TeacherSysWindow()
        self.new_teawindow.show()
    
    @pyqtSlot()
    def on_click_update_button(self):
        self.update_sub_window = QWidget()
        self.update_sub_window.setWindowTitle("修改学生信息")
        self.update_sub_window.move(800, 300)
        self.update_sub_window.resize(350, 250)

        self.update_table = QTableWidget(1, 3)
        self.update_table.setHorizontalHeaderLabels(['学院', '专业', '密码'])
        self.update_table_hbox = QHBoxLayout()
        self.update_table_hbox.addWidget(self.update_table)

        self.update_table_ok_btn = QPushButton("确认")
        self.update_table_ok_btn_hbox = QHBoxLayout()
        self.update_table_ok_btn_hbox.addWidget(self.update_table_ok_btn)

        self.update_sub_window_vbox = QVBoxLayout()
        self.update_sub_window_vbox.addLayout(self.update_table_hbox)
        self.update_sub_window_vbox.addLayout(self.update_table_ok_btn_hbox)

        self.update_sub_window.setLayout(self.update_sub_window_vbox)

        self.update_table_ok_btn.clicked.connect(self.on_click_update_ok_buttn)
        self.update_sub_window.show()

    @pyqtSlot()
    def on_click_update_ok_buttn(self):
        college = self.update_table.item(0, 0).text()
        major = self.update_table.item(0, 1).text()
        password = self.update_table.item(0, 2).text()

        new_stu = {
            "学院": college,
            "专业": major,
            "密码": password
        }

        stu_id, ok = QInputDialog.getText(self, '确认', '输入学生学号:')

        excel_operate_obj = ExcelOperate("./stuLists.xlsx")
        excel_operate_obj.update_byId("stuLists", stu_id, new_stu)

        self.update_sub_window.hide()
        self.hide()
        self.new_teawindow = TeacherSysWindow()
        self.new_teawindow.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = TeacherSysWindow()
    window.show()
    sys.exit(app.exec_())